﻿using System;
using System.Collections.Generic;
using Framework;
using UnityEngine;
using GameProtocols;

public class GameManager : Singleton<GameManager> {
    private int nextEntityID = 1;//从1开始，0代表没有entity
    public int NextEntityID
    {
        set
        {
            nextEntityID = value;
        }

        get
        {
            return nextEntityID++;
        }
    }

    Dictionary<int, ObjHandle<LogicEntity>> allEntities;
    List<ObjHandle<LogicEntity>> allEntitiesList;

    public IGame Game { private set; get; }

    public void Init(IGame game)
    {
        NextEntityID = 1;
        allEntities = new Dictionary<int, ObjHandle<LogicEntity>>();
        allEntitiesList = new List<ObjHandle<LogicEntity>>();

        Game = game;
        Game.Init();
    }

    public void Destroy()
    {
        Game.onDestroy();
        Game = null;

        foreach(var item in allEntitiesList)
        {
            if(item.IsValid)
            {
                item.handle.Destroy();
            }
        }

        allEntities = null;
        allEntitiesList = null;
    }

    public void Tick(PKG_FRAME frame)
    {
        Game.Update(frame);
        UpdateAllEntities();
    }

    public ObjHandle<LogicEntity> GetEntity(int EntityID)
    {
        if (allEntities == null)
            return ObjHandle<LogicEntity>.NULL;
        if (allEntities.ContainsKey(EntityID))
            return allEntities[EntityID];
        else
            return ObjHandle<LogicEntity>.NULL;
    }


    public int CreateFightCharacter(int characterID, long playerID)
    {
        //读表加载prefab
        int entityID = NextEntityID;
        VisualEntityManager.Instance.CreateEntity(characterID, entityID, playerID);

        ObjHandle<VisualEntity> vEntity = VisualEntityManager.Instance.GetEntity(entityID);
        if (vEntity.IsValid == false)
        {
            Debug.LogError("CreateEntityError");
            return 0;
        }

        LogicEntity logic = new FightCharacter();
        logic.characterID = characterID;
        logic.PlayerID = playerID;
        logic.EntityID = entityID;
        var self = new ObjHandle<LogicEntity>(logic);
        allEntities[entityID] = self;
        allEntitiesList.Add(self);
        logic.self = self;
        logic.Init();

        return entityID;
    }

    public int CreateSubItem(int characterID, long playerID)
    {
        //读表加载prefab
        int entityID = NextEntityID;
        VisualEntityManager.Instance.CreateEntity(characterID, entityID, playerID);

        ObjHandle<VisualEntity> vEntity = VisualEntityManager.Instance.GetEntity(entityID);
        if (vEntity.IsValid == false)
        {
            Debug.LogError("CreateEntityError");
            return 0;
        }

        LogicEntity logic = new SubItemCharacter();
        logic.characterID = characterID;
        logic.PlayerID = playerID;
        logic.EntityID = entityID;
        var self = new ObjHandle<LogicEntity>(logic);
        allEntities[entityID] = self;
        allEntitiesList.Add(self);
        logic.self = self;
        logic.Init();

        return entityID;
    }

    public Action<LogicEntity> DelEntityDelegation;
    public void DelEntity(int entityID)
    {
        ObjHandle<LogicEntity> logic = GetEntity(entityID);
        if (logic.IsValid)
        {
            long playerId = logic.handle.PlayerID;
            if(DelEntityDelegation != null)
                DelEntityDelegation.Invoke(logic.handle);
            logic.handle.Destroy();
            allEntities.Remove(entityID);
            logic.Release();
            VisualEntityManager.Instance.DelEntity(entityID);
        }

        allEntities.Remove(entityID);
        allEntitiesList.Remove(logic);
    }

    void UpdateAllEntities()
    {
        foreach(var logicEntity in allEntitiesList)
        {
            if (logicEntity.IsValid)
                logicEntity.handle.FixedUpdate();
        }
    }
}
